
#ifndef _THREAD_HAL_H_
#define _THREAD_HAL_H_

#ifndef _WIN32
#include <unistd.h>
#endif
#include <stdint.h>
#include <stddef.h>

#include <pthread.h>
#include <sched.h>

#define __DEBUG
#ifdef __DEBUG
#define HAL_ERR(format, ...)   	do{ printf("*** "format, ##__VA_ARGS__); }while(0)
#define HAL_WRN(format, ...)   	do{ printf("### "format, ##__VA_ARGS__); }while(0)
#define HAL_INFO(format, ...)   do{ printf("!!! "format, ##__VA_ARGS__); }while(0)
#define HAL_MSG(format, ...)   do{ }while(0)

#define HAL_ASSERT  			assert
#else

#define HAL_ERR  				do{ printf("*** "format, ##__VA_ARGS__); }while(0)
#define HAL_WRN(...)   			do{ printf("### "format, ##__VA_ARGS__); }while(0)
#define HAL_INFO(...)  			do{}while(0)
#define HAL_MSG(format, ...)   do{ }while(0)

#define HAL_ASSERT  			assert
#endif

#define HAL_SUCCESS             (0)
#define HAL_ERROR               (-1)
#define HAL_TIMEOUT             (-2)
#define HAL_INVALID_PARAM       (-3)
#define HAL_NO_DEVICE           (-4)
#define HAL_NO_CMD              (-5)
#define HAL_NO_CMD_SPACE        (-6)
#define HAL_SOCK_DISCONNET      (-7)
#define HAL_CONNECT_FAIL        (-8)
#define HAL_INVALID_CMD_REQ     (-9)
#define HAL_NO_MEMORY           (-10)
#define HAL_QUEUE_FULL          (-11)

/*error for modbus*/
#define HAL_MODBUS_ILLEGAL_REQ_ID   (-20)
#define HAL_MODBUS_ILLEGAL_CMD      (-21)
#define HAL_MODBUS_INVLAID_CMD      (-22)
#define HAL_MODBUS_ILLEGAL_DATA_LEN (-23)
#define HAL_MODBUS_CMD_NOT_ACCEPT   (-24)



/*error for json*/
#define HAL_JSON_NO_SUCH_LABEL          (-30)

#ifndef TRUE
#define TRUE  1
#endif

#ifndef FALSE
#define FALSE  0
#endif

#define DEFAULT_MIN_THREAD_STACK_SIZE   (48*1024)
#define DEFAULT_THREAD_SCHED_POLICY     SCHED_OTHER
#define DEFAULT_THREAD_PRIORITY         20
void *malloc_hal(int32_t size);
void  free_hal(void *mem);


typedef void* thread_t;
typedef void* semaphore_t;
typedef void* mutex_t;

thread_t thread_create(void (*entry_point)( void *), void *arg, int32_t stack_size, int prio);
void thread_exit(void);
thread_t thread_self(void);
void thread_time_sleep(int32_t  ms);


semaphore_t semaphore_create( int init_value );
void semaphore_destroy( semaphore_t sem );

void semaphore_signal(semaphore_t sem);
void semaphore_wait(semaphore_t sem);
int semaphore_wait_timeout(semaphore_t sem, int32_t timeMs);

mutex_t mutex_create(void);
void mutex_lock(mutex_t mutex);
void mutex_unlock(mutex_t mutex);
void mutex_destroy(mutex_t mutex);

#ifdef _WIN32
void sleep(int ms);
#endif

#endif

